package euler.helpers;

import static org.jlambda.util.Lists.last;

import java.math.BigInteger;

import org.jlambda.list.List;
import org.jlambda.list.MemoizingList;
import org.jlambda.listFunctions.ListFun0;

public class Fib extends ListFun0<BigInteger> {

	MemoizingList<BigInteger> list;
	
	public BigInteger getNext() {
		BigInteger a = (BigInteger) last( list, 2).toArray()[0];
		BigInteger b = last(list); 
		
		// lazy but only adds the last to the second last when needed
		return BigIntMath.addB.apply( a, b);
	}

	@Override
	protected void create(List<BigInteger> list) {
		this.list = (MemoizingList<BigInteger>) list;
//		this.list.append(0l);
//		this.list.append(1l);
		this.list.append(new BigInteger("0"));
		this.list.append(new BigInteger("1"));
	}

}
